package com.bosssoft.trainee.rbac.controller;

import com.bosssoft.trainee.rbac.entity.dto.FunsDTO;
import com.bosssoft.trainee.rbac.entity.vo.CommonResult;
import com.bosssoft.trainee.rbac.entity.vo.FunVO;
import com.bosssoft.trainee.rbac.service.FunctionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/fun")
public class FunctionController {
    @Autowired
    private FunctionService functionService;

    @PostMapping("/update")
    public CommonResult<FunVO> updateFunction(FunVO funVO) {
        FunsDTO funsDTO = new FunsDTO();
        BeanUtils.copyProperties(funVO,funsDTO);
        if(funsDTO != null){
           int result = functionService.updateFunction(funsDTO);
           if(result > 0){
               return new CommonResult(200, "插入成功", result);
           }
        }
        return new CommonResult(400, "插入失败", null);
    }

    @PostMapping("/insert")
    public CommonResult<FunVO> insertFunction(@RequestBody FunVO funVO) {
        FunsDTO funsDTO = new FunsDTO();
        BeanUtils.copyProperties(funVO,funsDTO);
        if(funsDTO != null){
            int result = functionService.insertFunction(funsDTO);
            if(result > 0){
                return new CommonResult(200, "插入成功", result);
            }
        }
        return new CommonResult(400, "插入失败", null);
    }

    @PostMapping("/delete")
    public CommonResult<FunVO> deleteFunction(@RequestBody FunVO funVO) {
        FunsDTO funsDTO = new FunsDTO();
        BeanUtils.copyProperties(funVO,funsDTO);
        if(funsDTO.getFunId() != null){
            int result = functionService.deleteFunction(funsDTO.getMenuId());
            if(result > 0){
                return new CommonResult(200, "删除成功", result);
            }
        }
        return new CommonResult(400, "删除失败", null);
    }

    @PostMapping("/get")
    public CommonResult<FunVO> getFunctionByMenuId(@RequestBody FunVO funVO) {

        FunsDTO funsDTO = new FunsDTO();
        BeanUtils.copyProperties(funVO,funsDTO);
        if(funsDTO.getFunId() != null){
            List<FunsDTO> funDTOList = functionService.getFunctionByMenuId(funsDTO.getMenuId());
            if(funDTOList != null){
                return new CommonResult(200, "查找成功", funDTOList);
            }
        }
        return new CommonResult(400, "查找失败", null);
    }
}


